#include <stdio.h>
#include <algorithm>
using namespace std;
int a[5005];
int tree[50000]
struct Node{
    int l,r,num;
}tree[50000];
void Build(int n,int x,int y){
    tree[n].l = x;
    tree[n].r = y;
    tree[n].num = 0;
    if(x == y){
        return;
    }
    int mid = (x + y) / 2;
    Build(2*n,x,mid);
    Build(2*n+1,mid+1,y);
}
void Modify(int n,int x){
    int l = tree[n].l;
    int r = tree[n].r;
    //printf("%d %d\n",l,r);
    int mid = (l + r) / 2;
    if(x == l && x == r){
        tree[n].num = 1;
        return;
    }
    if(x <= mid)    Modify(2*n,x);
    else            Modify(2*n+1,x);
    tree[n].num = tree[2*n].num + tree[2*n+1].num;
}
int Query(int n,int x,int y){
    int l = tree[n].l;
    int r = tree[n].r;
    int mid = (l + r) / 2;
    int ans = 0;;
    if(x == l && y == r)
        return tree[n].num;
	if(x <= mid)   ans += Query(2*n,x,min(mid,y));
    if(y > mid)    ans += Query(2*n+1,max(mid+1,x),y);
    return ans;
}

int main(){
    int n,sum,ans;
    int i,j;
    while(scanf("%d",&n) != EOF){
        sum = 0;
        Build(1,0,n);
        for(i = 1;i <= n;i++){
            scanf("%d",&a[i]);
            Modify(1,a[i]);
            sum += Query(1,a[i]+1,n);
        }
        ans = sum;
        for(i = 1;i < n;i++){
            sum = sum + (n - 1 - a[i]) - a[i];
            if(sum < ans)
                ans = sum;
        }
        printf("%d\n",ans);
    }

}

